package com.dingwen.minsta.task;

import com.dingwen.minsta.service.TempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 临时清洁任务
 * 临时目录清理任务
 *
 * @author dingwen
 * 2021.05.27 17:36
 * @date 2021/05/28
 */
@Component
public class TempCleanTask {
    private final TempService tempService;

    @Autowired
    public TempCleanTask(TempService tempService) {
        this.tempService = tempService;
    }

    /**
     * 清理
     *
     * @param date     执行时间
     * @param period   延迟多久执行 毫秒
     * @param duration 文件存活时间
     * @param dir      dir 临时文件夹
     */
    public void clean(Date date, Long period, Long duration, String dir) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                tempService.cleanTemp(dir, duration);
            }
        }, date, period);
    }

}
